/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.policies.pool;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.swt.graphics.Color;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

/**
 * 当鼠标进入时，高亮显示
 */
public class PoolHighlightEditPolicy extends
		org.eclipse.gef.editpolicies.GraphicalEditPolicy {

	private Color revertColor;

	public void eraseTargetFeedback(Request request) {
		if (revertColor != null) {
			setContainerBackground(revertColor);
			revertColor = null;
		}
	}

	private Color getContainerBackground() {
		return getContainerFigure().getBackgroundColor();
	}

	private IFigure getContainerFigure() {
		IFigure figure = ((GraphicalEditPart) getHost()).getContentPane();
		return figure;
	}

	public EditPart getTargetEditPart(Request request) {

		return request.getType().equals(RequestConstants.REQ_SELECTION_HOVER) ? getHost()
				: null;
	}

	private void setContainerBackground(Color c) {
		getContainerFigure().setBackgroundColor(c);
	}

	protected void showHighlight() {
		if (revertColor == null) {
			revertColor = getContainerBackground();
			setContainerBackground(Util4Eclipse.colorStr2ColorObj("#FFF5EE"));
		}
	}

	public void showTargetFeedback(Request request) {
		if (request.getType().equals(RequestConstants.REQ_CREATE)){
			CreateRequest createRequest = (CreateRequest)request;
			Object newObj = createRequest.getNewObject();
			if (Fpdl20DiagramElement.LANE.equals(newObj)){
				showHighlight();
			}
		}
		
//		if (request.getType().equals(
//						RequestConstants.REQ_CONNECTION_START)
//				|| request.getType()
//						.equals(RequestConstants.REQ_CONNECTION_END)
//				|| request.getType().equals(RequestConstants.REQ_CREATE))
//
//			showHighlight();
	}

}
